<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Auth;
class CommonController extends Controller {
	public function _initialize(){
		$module = strtolower(MODULE_NAME);
		$controller = strtolower(CONTROLLER_NAME);
		$path = strtolower(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
		if($module=='admin' && $controller!='login'){
			if(!isset($_SESSION['aid'])){
				if(IS_AJAX){
					$this->ajaxReturn(['error'=>3,'info'=>'请先登录','url'=>'admin/login/index']);
				}else{
					if($_FILES){
						$this->ajaxReturn(['code'=>3,'msg'=>'请先登录']);
					}else{
						$this->error('请先登录',U('admin/login/index'));
					}
				}
			}/*else{
				if($module=='admin' && $controller!='index'){
					$auth = new Auth();

					if(!$auth->check($path,$_SESSION['aid'])){
						$this->error('你没有权限，请与管理员联系',U('admin/welcome/index'));
					}
				}
			}*/
		}
	}

	public function deleteAll($id,$db,$isStatus=false){
		$ids = I('post.ids');

	    $ids= rtrim($ids,',');

	    $map[$id]=array('in',$ids);
	    
	    if($isStatus){
	    	$result = M($db)->where($map)->setField(['isdel'=>'1']);
	    }else{
	   		$result = M($db)->where($map)->delete();
	    }

	    if($result){
	        $this->ajaxReturn(['error'=>0,'info'=>'删除成功']);
	    }else{
	        $this->ajaxReturn(['error'=>1,'info'=>'删除失败']);
	    }
	}

	public function delOne($id,$db,$isStatus=false){
		$pid = I('post.id');
	    
	    if($isStatus){
	    	$result = M($db)->where([$id=>$pid])->setField(['isdel'=>'1']);
	    }else{
	   		$result = M($db)->where([$id=>$pid])->delete();
	    }

	    /*var_dump($result);exit;*/

	    if($result){
	        $this->ajaxReturn(['error'=>0,'info'=>'删除成功']);
	    }else{
	        $this->ajaxReturn(['error'=>1,'info'=>'删除失败']);
	    }
	}

	public function recoverOne($id,$db){
		$pid = I('post.id');
	    
	    $result = M($db)->where([$id=>$pid])->setField(['isdel'=>'0']);

	    /*var_dump($result);exit;*/

	    if($result){
	        $this->ajaxReturn(['error'=>0,'info'=>'恢复成功']);
	    }else{
	        $this->ajaxReturn(['error'=>1,'info'=>'恢复失败']);
	    }
	}
}